package edu.upc.rentacar.repositoryjdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import edu.upc.rentacar.domain.Auto;
import edu.upc.rentacar.repository.AutoRepository;

@Repository
public class AutoRepositoryJdbc extends JdbcDaoSupport implements AutoRepository {

	@Autowired
	public AutoRepositoryJdbc(DataSource datasource ){
		setDataSource(datasource);
	}
		
	
	public List<Auto> getAll() {
		List<Auto> autos;
		String sql = "SELECT * FROM AUTO";
		autos = getJdbcTemplate().query(sql, new AutoRowMapper());
		
		return autos;

	}

	private class AutoRowMapper implements ParameterizedRowMapper<Auto>{
		public Auto mapRow(ResultSet rs, int rowNum) throws SQLException{
			Auto p = new Auto();
			p.setId(rs.getLong("id"));
			p.setAno(rs.getString("ano"));
			p.setMarca(rs.getString("marca"));
			p.setPlaca(rs.getString("placa"));
			return p;
		}
	}	
	
	
}
